#include "STC8H.H"
#include "SSD1315.h"
#include <string.h>

#define _ROTATE_ 1 //旋转显示
//字库
code const uint8_t font_data[][16] = {   
    {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*' ',0*/
      },
      {
        0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0x00,0x00,0x00, /*'!',1*/
      },
      {
        0x00,0x10,0x0C,0x02,0x10,0x0C,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*'"',2*/
      },
      {
        0x00,0x40,0xC0,0x78,0x40,0xC0,0x78,0x00,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x00, /*'#',3*/
      },
      {
        0x00,0x70,0x88,0x88,0xFC,0x08,0x30,0x00,0x00,0x18,0x20,0x20,0xFF,0x21,0x1E,0x00, /*'$',4*/
      },
      {
        0xF0,0x08,0xF0,0x80,0x60,0x18,0x00,0x00,0x00,0x31,0x0C,0x03,0x1E,0x21,0x1E,0x00, /*'%',5*/
      },
      {
        0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x2C,0x19,0x27,0x21,0x10, /*'&',6*/
      },
      {
        0x00,0x12,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*''',7*/
      },
      {
        0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00, /*'(',8*/
      },
      {
        0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00, /*')',9*/
      },
      {
        0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00, /*'*',10*/
      },
      {
        0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x0F,0x01,0x01,0x01, /*'+',11*/
      },
      {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x90,0x70,0x00,0x00,0x00,0x00,0x00, /*',',12*/
      },
      {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00, /*'-',13*/
      },
      {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00, /*'.',14*/
      },
      {
        0x00,0x00,0x00,0x00,0xC0,0x38,0x04,0x00,0x00,0x60,0x18,0x07,0x00,0x00,0x00,0x00, /*'/',15*/
      },
      {
        0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00, /*'0',16*/
      },
      {
        0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00, /*'1',17*/
      },
      {
        0x00,0x70,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00, /*'2',18*/
      },
      {
        0x00,0x30,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x18,0x20,0x21,0x21,0x22,0x1C,0x00, /*'3',19*/
      },
      {
        0x00,0x00,0x80,0x40,0x30,0xF8,0x00,0x00,0x00,0x06,0x05,0x24,0x24,0x3F,0x24,0x24, /*'4',20*/
      },
      {
        0x00,0xF8,0x88,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x20,0x20,0x20,0x11,0x0E,0x00, /*'5',21*/
      },
      {
        0x00,0xE0,0x10,0x88,0x88,0x90,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x20,0x1F,0x00, /*'6',22*/
      },
      {
        0x00,0x18,0x08,0x08,0x88,0x68,0x18,0x00,0x00,0x00,0x00,0x3E,0x01,0x00,0x00,0x00, /*'7',23*/
      },
      {
        0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00, /*'8',24*/
      },
      {
        0x00,0xF0,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x01,0x12,0x22,0x22,0x11,0x0F,0x00, /*'9',25*/
      },
      {
        0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00, /*':',26*/
      },
      {
        0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x00, /*';',27*/
      },
      {
        0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00, /*'<',28*/
      },
      {
        0x00,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x02,0x02,0x02,0x02,0x02,0x02,0x00, /*'=',29*/
      },
      {
        0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00, /*'>',30*/
      },
      {
        0x00,0x70,0x48,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x30,0x37,0x00,0x00,0x00, /*'?',31*/
      },
      {
        0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x28,0x2F,0x28,0x17,0x00, /*'@',32*/
      },
      {
        0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20, /*'A',33*/
      },
      {
        0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00, /*'B',34*/
      },
      {
        0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00, /*'C',35*/
      },
      {
        0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00, /*'D',36*/
      },
      {
        0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00, /*'E',37*/
      },
      {
        0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00, /*'F',38*/
      },
      {
        0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00, /*'G',39*/
      },
      {
        0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20, /*'H',40*/
      },
      {
        0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00, /*'I',41*/
      },
      {
        0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00, /*'J',42*/
      },
      {
        0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00, /*'K',43*/
      },
      {
        0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00, /*'L',44*/
      },
      {
        0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x01,0x3E,0x01,0x3F,0x20,0x00, /*'M',45*/
      },
      {
        0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00, /*'N',46*/
      },
      {
        0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00, /*'O',47*/
      },
      {
        0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00, /*'P',48*/
      },
      {
        0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x28,0x28,0x30,0x50,0x4F,0x00, /*'Q',49*/
      },
      {
        0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20, /*'R',50*/
      },
      {
        0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00, /*'S',51*/
      },
      {
        0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00, /*'T',52*/
      },
      {
        0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00, /*'U',53*/
      },
      {
        0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00, /*'V',54*/
      },
      {
        0x08,0xF8,0x00,0xF8,0x00,0xF8,0x08,0x00,0x00,0x03,0x3E,0x01,0x3E,0x03,0x00,0x00, /*'W',55*/
      },
      {
        0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20, /*'X',56*/
      },
      {
        0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00, /*'Y',57*/
      },
      {
        0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00, /*'Z',58*/
      },
      {
        0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00, /*'[',59*/
      },
      {
        0x00,0x04,0x38,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00, /*'\',60*/
      },
      {
        0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00, /*']',61*/
      },
      {
        0x00,0x00,0x04,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*'^',62*/
      },
      {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, /*'_',63*/
      },
      {
        0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*'`',64*/
      },
      {
        0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x19,0x24,0x24,0x12,0x3F,0x20,0x00, /*'a',65*/
      },
      {
        0x10,0xF0,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00, /*'b',66*/
      },
      {
        0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00, /*'c',67*/
      },
      {
        0x00,0x00,0x80,0x80,0x80,0x90,0xF0,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20, /*'d',68*/
      },
      {
        0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x24,0x24,0x24,0x24,0x17,0x00, /*'e',69*/
      },
      {
        0x00,0x80,0x80,0xE0,0x90,0x90,0x20,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00, /*'f',70*/
      },
      {
        0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00, /*'g',71*/
      },
      {
        0x10,0xF0,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20, /*'h',72*/
      },
      {
        0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00, /*'i',73*/
      },
      {
        0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00, /*'j',74*/
      },
      {
        0x10,0xF0,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x06,0x29,0x30,0x20,0x00, /*'k',75*/
      },
      {
        0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00, /*'l',76*/
      },
      {
        0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F, /*'m',77*/
      },
      {
        0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20, /*'n',78*/
      },
      {
        0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00, /*'o',79*/
      },
      {
        0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0x91,0x20,0x20,0x11,0x0E,0x00, /*'p',80*/
      },
      {
        0x00,0x00,0x00,0x80,0x80,0x00,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0x91,0xFF,0x80, /*'q',81*/
      },
      {
        0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00, /*'r',82*/
      },
      {
        0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00, /*'s',83*/
      },
      {
        0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x10,0x00, /*'t',84*/
      },
      {
        0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20, /*'u',85*/
      },
      {
        0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x03,0x0C,0x30,0x0C,0x03,0x00,0x00, /*'v',86*/
      },
      {
        0x80,0x80,0x00,0x80,0x80,0x00,0x80,0x80,0x01,0x0E,0x30,0x0C,0x07,0x38,0x06,0x01, /*'w',87*/
      },
      {
        0x00,0x80,0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x20,0x31,0x0E,0x2E,0x31,0x20,0x00, /*'x',88*/
      },
      {
        0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x81,0x86,0x78,0x18,0x06,0x01,0x00, /*'y',89*/
      },
      {
        0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00, /*'z',90*/
      },
      {
        0x00,0x00,0x00,0x00,0x00,0xFC,0x02,0x02,0x00,0x00,0x00,0x00,0x01,0x3E,0x40,0x40, /*'{',91*/
      },
      {
        0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00, /*'|',92*/
      },
      {
        0x02,0x02,0xFC,0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x3E,0x01,0x00,0x00,0x00,0x00, /*'}',93*/
      },
      {
        0x00,0x02,0x01,0x02,0x02,0x04,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*'~',94*/
      },
      {
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /*'',95*/
      },
};

#pragma region 显示
// 硬件I2C初始化
void I2C_Init() {
    _I2C_IO_INIT_();
    I2CCFG |= (FOSC_SPEED / 2 / I2C_SPEED - 4) / 2; // 时钟分频计算
    I2CCFG |= 0xc0;  // 使能I2C主机模式
    I2CMSST = 0x00; // 清除状态寄存器
 //   I2CMSAUX = 0x00;
}

void I2C_Wait(){
    while (!(I2CMSST & 0x40)); // 等待传输完成
    I2CMSST &= ~0x40; // 清除标志位
}

// I2C启动传输
void I2C_Start() {
    I2CMSCR = 0x01;  // 发送START条件
    I2C_Wait();
}

// I2C停止传输
void I2C_Stop() {
    I2CMSCR = 0x06;  // 发送STOP条件
    I2C_Wait();
}

void Recv_ACK(){
    I2CMSCR = 0x03;
    I2C_Wait();
}

// 发送单字节
void I2C_SendByte(uint8_t dat) {
    I2CTXD = dat;
    I2CMSCR = 0x02;  // 发送数据
    I2C_Wait();
    Recv_ACK();
}

// 发送SSD1315命令
void SSD1315_WriteCmd(uint8_t cmd) {
    I2C_Start();
    I2C_SendByte(SSD1315_ADDR << 1); // 写模式
    I2C_SendByte(0x00);              
    I2C_SendByte(cmd);
    I2C_Stop();
}
// 初始化SSD1315
void SSD1315_Init() {
    SSD1315_WriteCmd(DISPLAY_OFF);         // 关闭显示
    SSD1315_WriteCmd(0x20);              // 设置存储器寻址模式
    SSD1315_WriteCmd(0x00);              // 00,水平寻址模式;01,垂直寻址模式;10,页面寻址模式(B0-B7)

    SSD1315_WriteCmd(0xd5);              // 设置显示时钟分频比/振荡器频率
    SSD1315_WriteCmd(0x80);              // 分频比,建议设置为80h

    SSD1315_WriteCmd(0xa8);              // 设置多路复用率(行数)
    SSD1315_WriteCmd(0x3f);              // 1-63

    SSD1315_WriteCmd(0xd3);              // 设置显示偏移
    SSD1315_WriteCmd(0x00);              // 偏移量

    SSD1315_WriteCmd(0x40);              // 设置显示开始行
    if(!_ROTATE_)
    {
        SSD1315_WriteCmd(0xC8);          // 设置起始行地址为63,// 设置COM输出扫描方向// C0:从上到下扫描;C8:从下到上扫描
        SSD1315_WriteCmd(0xa1);              // 设置段重新映射
    } else {
        SSD1315_WriteCmd(0xC0);          // 设置起始行地址为0
        SSD1315_WriteCmd(0xa0);              // 设置段重新映射// A0:列地址0映射到SEG0;A1:列地址127映射到SEG0
    }

    SSD1315_WriteCmd(0xda);              // 设置COM引脚硬件配置
    SSD1315_WriteCmd(0x12);              

    SSD1315_WriteCmd(0x81);              // 设置对比度控制
    SSD1315_WriteCmd(0xcf);              

    SSD1315_WriteCmd(0xd9);              // 设置预充电周期
    SSD1315_WriteCmd(0xf1);              

    SSD1315_WriteCmd(0xdb);              // 设置VCOMH取消选择级别
    SSD1315_WriteCmd(0x30);              

    SSD1315_WriteCmd(0xa4);              // 设置整个显示打开/恢复到正常显示
                                             // (A4: 正常; A5: 全屏显示)

    SSD1315_WriteCmd(0xa6);              // 设置正常/倒置显示
                                             // (A6: 正常; A7: 反色显示)

    SSD1315_WriteCmd(0x8d);              // 使能电荷泵
    SSD1315_WriteCmd(0x14);              // 电荷泵使能

    SSD1315_WriteCmd(DISPLAY_ON);         // 开启显示
}

void ScreenTest() {
    uint16_t i ,j ;
    for(i=0;i<8;i++){
    I2C_Start();
    I2C_SendByte(SSD1315_ADDR << 1);
    I2C_SendByte(0x00);          // 命令模式
    I2C_SendByte(0xB0 | i);      // 设置页地址(0-7)
    I2C_SendByte(0x0f & 0);          // 设置列地址低4位 
    I2C_SendByte(0x10 + (0>>4));          // 设置列地址高4位 
    I2C_Stop();
    
    I2C_Start();
    I2C_SendByte(SSD1315_ADDR << 1);
    I2C_SendByte(0x40);          // 数据模式
    
    // 从第5列开始显示，所以只需要显示123列
    for ( j = 0; j < 128; j++)
    {
        I2C_SendByte(0x00); // 发送数据
    }
    
    I2C_Stop();
}
}

void Display(uint8_t _page, uint8_t _col, const uint8_t *_buf, uint16_t _size){
    uint16_t i;
        I2C_Start();
        I2C_SendByte(SSD1315_ADDR << 1);
        I2C_SendByte(0x00);          // 命令模式
        I2C_SendByte(0xB0 | _page);      // 设置页地址(0-7)
        I2C_SendByte(0x0f & _col);          // 设置列地址低4位 
        I2C_SendByte(0x10 + (_col>>4));          // 设置列地址高4位 
        I2C_Stop();
        
        I2C_Start();
        I2C_SendByte(SSD1315_ADDR << 1);
        I2C_SendByte(0x40);          // 数据模式
        
        // 从第5列开始显示，所以只需要显示123列
        for ( i = 0; i < _size; i++)
        {
            I2C_SendByte(_buf[i]); // 发送数据
        }
        
        I2C_Stop();
    
}
void DisplayString(const char* str, uint8_t page, uint8_t column, uint8_t lineWidth, bit center, bit _inverse) {
    uint8_t buf_up[128] = {0};
    uint8_t buf_down[128] = {0};
    uint16_t len = strlen(str);
    uint16_t i, j;
    uint8_t offset = 0;
    // Calculate size needed based on string length (each char is 8 pixels wide)
    uint16_t size = len * 8;
    if(size > lineWidth) size = lineWidth;
    

    if(center) {
        offset = (lineWidth - size) / 2;
    }
    
    // Build display buffer from font data
    for(i = 0; i < len && (i*8) < size; i++) {
        // Get font data for current character
        uint8_t c = str[i] - 32; // Adjust for font table offset
        if(c > 95) c = 0; // Use space for invalid chars
        
        // Copy upper 8 bytes of font data for this character
        for(j = 0; j < 8 && (i*8 + j) < size; j++) {
            if(_inverse) {
                buf_up[i*8 + j] = ~font_data[c][j]; // Inverse the font data
                buf_down[i*8 + j] = ~font_data[c][j + 8];
            } else {
                buf_up[i*8 + j] = font_data[c][j];
                buf_down[i*8 + j] = font_data[c][j + 8];
            }
        }
    }
    
    // Display the upper and lower parts at specified position
    Display(page, column + offset, buf_up, size);
    Display(page + 1, column + offset, buf_down, size);
}
/*void DisplayString(const char* str, uint8_t page, uint8_t column, bit _inverse) {
    uint8_t buf_up[128] = {0};
    uint8_t buf_down[128] = {0};
    uint16_t len = strlen(str);
    uint16_t i, j;
    
    // Calculate size needed based on string length (each char is 8 pixels wide)
    uint16_t size = len * 8;
    if(size > 128) size = 128;
    
    // Build display buffer from font data
    for(i = 0; i < len && (i*8) < size; i++) {
        // Get font data for current character
        uint8_t c = str[i] - 32; // Adjust for font table offset
        if(c > 95) c = 0; // Use space for invalid chars
        
        // Copy upper 8 bytes of font data for this character
        for(j = 0; j < 8 && (i*8 + j) < size; j++) {
            if(_inverse) {
                buf_up[i*8 + j] = ~font_data[c][j]; // Inverse the font data
                buf_down[i*8 + j] = ~font_data[c][j + 8];
            } else {
                buf_up[i*8 + j] = font_data[c][j];
                buf_down[i*8 + j] = font_data[c][j + 8];
            }
        }
    }
    
    // Display the upper and lower parts at specified position
    Display(page, column, buf_up, size);
    Display(page + 1, column, buf_down, size);
}

*/
void DisplayChar(char ch, uint8_t page, uint8_t column, bit _inverse) {
    uint8_t buf_up[8] = {0};
    uint8_t buf_down[8] = {0};
    uint8_t c = ch - 32; // Adjust for font table offset
    uint8_t j;
    
    if(c > 95) c = 0; // Use space for invalid chars
    
    // Copy upper 8 bytes of font data for this character
    for(j = 0; j < 8; j++) {
        if(_inverse) {
            buf_up[j] = ~font_data[c][j]; // Inverse the font data
            buf_down[j] = ~font_data[c][j + 8];
        } else {
            buf_up[j] = font_data[c][j];
            buf_down[j] = font_data[c][j + 8];
        }
    }
    
    // Display the upper and lower parts at specified position
    Display(page, column, buf_up, 8);
    Display(page + 1, column, buf_down, 8);
}
#pragma endregion 显示